<template>
    <div>
        <border-header second="订单管理" third="订单列表"></border-header>

        <el-card>
            <el-row>
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query">
                        <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
                    </el-input>
                </el-col>
            </el-row>

            <el-table :data="orderList" style="width: 100%" stripe border>
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="order_number" label="订单编号" width="400"></el-table-column>
                <el-table-column prop="order_price" label="订单价格"></el-table-column>
                <el-table-column label="是否付款">
                    <template slot-scope="scope">
                        <el-tag type="danger" v-if="scope.row.order_pay==='0'">未付款</el-tag>
                        <el-tag v-else>已付款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="is_send" label="是否发货"></el-table-column>
                <el-table-column prop="create_time" label="下单时间"></el-table-column>
                <el-table-column label="操作">
                    <template>
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showBox"></el-button>
                        <el-button type="success" icon="el-icon-location" size="mini" @click="showProgressBox"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页器 -->
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[5, 10, 15]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
        </el-card>

        <!-- 修改地址的对话框 -->
        <el-dialog
        title="修改地址"
        :visible.sync="addressVisible"
        width="50%" @close="addressDialogClosed">
        <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
            <el-form-item label="省市区/县" prop="address1">
                <el-cascader v-model="addressForm.address1" :options="citdata" :props="{ expandTrigger: 'hover' }"></el-cascader>
            </el-form-item>
            <el-form-item label="详细地址" prop="address2">
                <el-input v-model="addressForm.address2"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addressVisible = false">取 消</el-button>
            <el-button type="primary" @click="addressVisible = false">确 定</el-button>
        </span>
        </el-dialog>
        <!-- 物流进度的对话框 -->
        <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
            <el-timeline :reverse="false">
                <el-timeline-item
                v-for="(activity, index) in progressInfo"
                :key="index"
                :timestamp="activity.ftime">
                {{activity.context}}
                </el-timeline-item>
            </el-timeline>
        </el-dialog>
    </div>
</template>

<script>
import BorderHeader from '../BorderHeader.vue'
import http from '@/util/http'
import moment from 'moment'
import citdata from '@/components/orders/citdata'
export default {
  data () {
    return {
      // 获取订单查询的数据列表
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      // 订单列表数据
      orderList: [],
      // 级联选择器的数据源
      citdata: citdata,
      // 修改地址的对话框的显示与隐藏
      addressVisible: false,
      // 表单数据绑定的对象
      addressForm: {
        address1: [],
        address2: ''
      },
      // 验证规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 物流进度对话框的显示与隐藏
      progressVisible: false,
      // 物流进度信息
      progressInfo: []
    }
  },
  components: {
    BorderHeader
  },
  created () {
    this.getOrderList()
  },
  methods: {
    // 获取订单查询的数据
    async getOrderList () {
      const { data: res } = await http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取订单列表失败')
      res.data.goods.forEach(item => {
        item.create_time = moment(item.create_time).format('YYYY-MM-DD')
      })
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 一页显示多少条发生变化时触发的函数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 当前页改变时触发的函数
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 点击修改时触发的函数
    showBox () {
      this.addressVisible = true
    },
    // 监听修改地址对话框关闭的事件
    addressDialogClosed () {
      this.$refs.addressFormRef.resetFields()
    },
    // 查看物流进度的事件
    async showProgressBox () {
      // 获取物流信息
      const { data: res } = await http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error('获取物流进度失败')
      this.progressInfo = res.data
      this.progressVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {width: 100%;}
</style>
